﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.LocalConnection;
	import flash.system.ApplicationDomain;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	import sourceproxy.ResourceProxy;
	import sourceproxy.ResourceProxy;
	import sourceproxy.ResourceProxyEventType;
	import style.TextStyle;
	import utils.Zoom9Grid;
	
	/**
	 * ...
	 * @author Jun
	 */
	[SWF(width="800", height="600", backgroundColor="#FFFFFF")]
	public class Main extends Sprite 
	{
		private var win:MMBasicWindow;
		private var bmp2:Bitmap;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private var scrollBar:MMScrollBar = new MMScrollBar();
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			MangMeiMgr.initRoot(this);
			
			ResourceProxy.instance().push("/controls.swf","../bin");
			ResourceProxy.instance().push("/images/19.jpg");
			ResourceProxy.instance().push("/images/20.jpg");
			ResourceProxy.instance().push("/images/25.jpg");
		
			ResourceProxy.instance().push("/images/23.jpg");
			ResourceProxy.instance().push("/images/27.jpg");
			ResourceProxy.instance().push("/controls/radio_button_1.png");
			ResourceProxy.instance().push("/controls/radio_button_2.png");
			ResourceProxy.instance().push("/images/222.swf");
			ResourceProxy.instance().push("/images/dddd.swf");
			ResourceProxy.instance().push("/images/combobox.jpg");
			
			ResourceProxy.instance().push("/ui/common/sound_off.png");
			ResourceProxy.instance().push("/ui/common/sound_on.png");
			ResourceProxy.instance().push("/ui/common/btn_2_260.png");
			scrollBar.regResource();
			ResourceProxy.instance().onProgress = function (loadedCount:int, total:int):void
			{
				//trace(loadedCount);
			}
			ResourceProxy.instance().onComplete = todo;
			ResourceProxy.instance().load();
			
			stage.addEventListener(MouseEvent.CLICK, onClickHandler);
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
		}
		
		private function todo():void
		{
			ResourceProxy.instance().removeEventListener(ResourceProxyEventType.COMPLETE, todo);
			
			var bmp:Bitmap = new Bitmap(ResourceProxy.getBitmapData("/images/222.swf"));
			//addChild(bmp);
			
			var mc:MovieClip = ResourceProxy.getMovieClip("/images/dddd.swf");
			//addChild(mc);
			
			//removeChild(mc);
			//mc = null;
			//ResourceProxy.remove("/images/dddd.swf");
			//var bmpdata:BitmapData = new BitmapData(mc.width, mc.height, false, 0xffffff);
			//bmpdata.draw(mc,null,new ColorTransform(1,1,1,1,0xFF,-255,-255,0xFF));
			
			var label:MMLabel = new MMLabel();
			addChild(label);
			label.show();
			label.place(250, 140);
			label.text = "我爱你啊！";
			label.textStyle = new TextStyle(new TextFormat(null, 16, 0xff0000),[new GlowFilter(0x00FF00,1,6,6)]);
			
			removeChild(label);
			
			label = null;
		
			var label2:MMLabel = new MMLabel();
			addChild(label2);
			label2.show();
			label2.place(250, 100);
			label2.text = "这是一个单行本文。";
			label2.textStyle = new TextStyle(new TextFormat(null, 16, 0xff0000),[new GlowFilter(0x00FF00,1,6,6)]);
			
			var text:MMText = new MMText(150, 40);
			addChild(text);	
			text.place(250, 130);
			text.show();
			text.textStyle = new TextStyle(new TextFormat(null, 16, 0xff0000),[new GlowFilter(0x00FF00,1,6,6)]);
			text.text = "这是一个定义宽高的多行本行。";
			text.selectable = true;
			text.textWidth = 355;
			
			win = new MMBasicWindow();
			win.place(200, 300);
			win.canDrag = true;
			win.width = 200;
			win.height = 200;
			
			win.show();
			
			var img:MMImage = new MMImage();
			img.source = "/images/21.jpg";
			img.show();
			
			addChild(img);
			
			
			var btn:MMButton = new MMButton();
			btn.label = "确定";
			btn.backgroupImageUrl = "btn_2_260";
			addChild(btn);
			btn.place(300, 20);
			btn.show();
			btn.buttonMode = true;
			btn.textAlign = MMButton.TEXTALIGN_CENTER;
			btn.textStyle = TextStyle.whiteGlowBlack;
			btn.onClick = function ():void
			{
				
				trace("哈喽");
			}
			
			
			var checkbox:MMCheckBox = new MMCheckBox(ResourceProxy.getBitmapData("/ui/common/sound_on.png"), ResourceProxy.getBitmapData("/ui/common/sound_off.png") );
			checkbox.label = "CheckBox";
			checkbox.place(400, 20);
			checkbox.show();
			checkbox.onChange = function():void
			{
				trace(this.selected);
			}
			addChild(checkbox);
			
			var linkBtn:MMLinkButton = new MMLinkButton();
			linkBtn.label = "连接按钮";
			linkBtn.show();
			linkBtn.underline = true;
			addChild(linkBtn);
			linkBtn.place(500, 10);
			linkBtn.onClick = function():void
			{
				trace("linkButton");
			}
			linkBtn.addEventListener(FocusEvent.FOCUS_IN, function(e:Event):void { trace("按钮获得焦点"); })
			linkBtn.addEventListener(FocusEvent.FOCUS_OUT, function(e:Event):void { trace("按钮失去焦点"); })
			
			
			var combobox:Zoom9Grid = new Zoom9Grid(ResourceProxy.getBitmapData("/images/combobox.jpg"), new Rectangle(3, 3, 10, 10));
			combobox.width = 200;
			addChild(combobox.bitmap);
			
			scrollBar.place(20, 50);
			scrollBar.height = 200;
			scrollBar.show();
			
			//scrollBar.maxPosition = 5;
			addChild(scrollBar);
			scrollBar.maxPosition =10;
			scrollBar.onChange = function (pos:int):void
			{
				trace(pos);
			}
			
			var textinput:MMTextInput = new MMTextInput(10);
			
			textinput.place(20, 300);
			textinput.text = "数字。。。";
			textinput.border = true;
			
			textinput.show();
			textinput.onChange = function():void { trace("change"); }
			textinput.onEnter = function():void { trace("enter"); }
			addChild(textinput);
			
			var radioBtn:MMRadioButton = new MMRadioButton();
			radioBtn.label = "按钮1";
			radioBtn.value = "1";
			radioBtn.show();
			radioBtn.place(100, 30);
			addChild(radioBtn);
			
			var byte:ByteArray = new ByteArray();
			byte[0] = 1;
			byte[1] = 2;
			byte[2] = 3;
			byte.position = 1;
			//trace(byte[1]);
			trace(byte.readByte());
			//trace(byte.readInt() );
			
			//var bmd:BitmapData = new BitmapData(bmp.width, bmp.height, true, 0x00000000);
			//
			//bmd.threshold(bmp.bitmapData, new Rectangle(0, 0, bmp.bitmapData.width, bmp.bitmapData.height), new Point(), ">", 0x00000000, 0x80000000);
			//bmpdata.copyPixels(bmpdata, bmpdata.rect, new Point(), bmd, new Point(bmp2.x,bmp2.y), false);
			//
			//var bmp3:Bitmap = new Bitmap(bmd);
			//bmp3.x = 100;
			//bmp3.y = 300;
			//addChild(bmp3);
			compelGC();
		}
		
		private function onEnterFrame(e:Event):void 
		{
			//trace(int((getTimer() % 1000) / 5));
			
		}
		
		private function onClickHandler(e:MouseEvent):void 
		{
			//bmp2.x = e.stageX;
			//bmp2.y = e.stageY;
			//try
			//{
				//new LocalConnection().connect("local");
				//new LocalConnection().connect("local");
				//
			//}catch (e:*)
			//{
				//
			//}
		}
		
		static public function compelGC():void
		{
			System.gc();
			try 
			{
				new LocalConnection().connect("foo");
				new LocalConnection().connect("foo");
			}
			catch (e:*)
			{
				
			}
		}
	}
	
}